package com.chun.controller;

import com.chun.pojo.*;
import com.chun.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/students")
public class StudentController {
    private static final Logger logger = LoggerFactory.getLogger(StudentController.class);

    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result getStudents(String name, Integer degree, Integer clazzId,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "5") Integer pageSize) {
        PageBean pageBean = studentService.getStudents(name, degree, clazzId, page, pageSize);
        logger.info("分页查询学生信息");
        return Result.success(pageBean);
    }

    @DeleteMapping("/{ids}")
    public Result deleteStudentByIds(@PathVariable Integer[] ids) {
        studentService.deleteStudentByIds(ids);
        logger.info("根据id删除学生");
        return Result.success();
    }

    @PostMapping
    public Result addStudent(@RequestBody Student student) {
        studentService.addStudent(student);
        logger.info("添加学生");
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getStudentById(@PathVariable Integer id) {
       Student student = studentService.getStudentById(id);
       logger.info("根据id查询学生");
       return Result.success(student);
    }

    @PutMapping
    public Result updateStudent(@RequestBody Student student) {
        studentService.updateStudent(student);
        logger.info("更新学生信息");
        return Result.success();
    }

    @PutMapping("/{id}/{score}")
    public Result updateStudentScore(@PathVariable Integer id, @PathVariable Integer score) {
        studentService.updateStudentScore(id,score);
        logger.info("更新学生违纪分");
        return Result.success();
    }

    @GetMapping("/report/studentDegreeData")
    public Result getStudentDegreeData() {
        List<DegreeCountDTO> degreeCountDTOList = studentService.getStudentDegreeData();
        logger.info("统计学历");
        return Result.success(degreeCountDTOList);
    }

    @GetMapping("/report/studentCountData")
    public Result getStudentCountData() {
        List<StudentCountDTO> studentCountDTO = studentService.getStudentCountData();
        logger.info("统计班级对应的学生数");
        return Result.success(studentCountDTO);
    }

}
